package visual;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import plug.AleaNbPlant;
import plug.ColorCube;
import plug.CreaturePluginFactory;
import plug.NbProportional;
import plug.UserChoice;
import creatures.IColorStrategy;
import creatures.IStrategyNbPlants;
import creatures.visual.CreatureSimulator;


@SuppressWarnings("serial")
public class ChoiceFrame extends JDialog{
	private Collection<ChoicePanel> listChoicePanel;
	
	public ChoiceFrame(JFrame parent, CreatureSimulator simulator,Collection<String> pluginsNames) {
		super(parent,"Start configuration",true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		
		listChoicePanel = new ArrayList<ChoicePanel>();
	
		create(simulator,pluginsNames);
	}

	private void create(final CreatureSimulator simulator,final Collection<String> pluginNames) {
		final JPanel principale = new JPanel();
		principale.setLayout(new BoxLayout(principale, BoxLayout.PAGE_AXIS));
		
		for (String name : pluginNames) {
			ChoicePanel c = new ChoicePanel(name);
			principale.add(c);
			listChoicePanel.add(c);
		}
		
		JButton validate = new JButton("OK");
		validate.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				CreaturePluginFactory cpf = CreaturePluginFactory.getInsance();
				IColorStrategy cs = new ColorCube(50);
			
				for(ChoicePanel cp : listChoicePanel) {
						IStrategyNbPlants strategyNbPlant;
						String choice = cp.getGroup().getSelection().getActionCommand();
						
						if(choice.equals(ChoicePanel.userChoice))
							strategyNbPlant= new UserChoice(cp.getValueSliderChoiceNb());
						else if (choice.equals(ChoicePanel.aleatoire))
							strategyNbPlant = new AleaNbPlant();
						else //ChoicePanel.proba
							strategyNbPlant = new NbProportional(simulator.getSurfaceArea(),cp.getValueSliderPercentage());
				
						simulator.addAllCreatures(cpf.createCreatures(simulator, strategyNbPlant.getNbPlant(), cs, cp.getTitle()));
				}
				dispose();
			}
		});
		
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				
			}
		});
		
		JPanel buttonPan = new JPanel();
		buttonPan.setLayout(new BoxLayout(buttonPan, BoxLayout.LINE_AXIS));
		buttonPan.add(validate);
		buttonPan.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPan.add(cancel);
		principale.add(buttonPan);
		
		JScrollPane scroll = new JScrollPane(principale,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		add(scroll);
		
		setResizable(false);
		setPreferredSize(new Dimension(640, 320));
		pack();
	}
	
}
